home *** CD-ROM | disk | FTP | other *** search
/ An Introduction to Progr…l Basic 6.0 (4th Edition) / An Introduction to Programming using Visual Basic 6.0.iso / COMMON / TOOLS / VB / CABINETS / MSDAO350.CAB / icontrols / MaskedEdit / Mask.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-01-08  |  4.2 KB  |  198 lines

  1. package icontrols.MaskedEdit;
  2.  
  3. public class Mask {
  4.    public static final int LEFT = 1;
  5.    public static final int RIGHT = 2;
  6.    public static final char DIGIT_REQUIRED = '#';
  7.    public static final char CHARACTER = '&';
  8.    public static final char ALPHANUMERIC_REQUIRED = 'A';
  9.    public static final char ALPHANUMERICSPACE = 'a';
  10.    public static final char ALPHA = '?';
  11.    public static final char DIGITPLUSMINUSSPACE = '9';
  12.    public static final char FORCE_UPPER = '>';
  13.    public static final char FORCE_LOWER = '<';
  14.    public static final char ESCAPE = '\\';
  15.    private static final char LITERAL = 'L';
  16.    private static final char NOCASE = '?';
  17.    private static final char UPPER = 'U';
  18.    private static final char LOWER = 'L';
  19.    private int len;
  20.    private String mask;
  21.    private char[] maskEmpty;
  22.    private char[] maskCompiled;
  23.    private char[] maskCase;
  24.    private char promptChar;
  25.  
  26.    public boolean isMetaChar(int index) throws IllegalArgumentException {
  27.       if (index >= 0 && index < this.length()) {
  28.          return this.maskCompiled[index] != 'L';
  29.       } else {
  30.          throw new IllegalArgumentException("Illegal Index");
  31.       }
  32.    }
  33.  
  34.    public boolean isValidCharacter(int index, char c, boolean ignorePrompt) throws IllegalArgumentException {
  35.       if (index >= 0 && index < this.length()) {
  36.          if (ignorePrompt && this.isequalPromptChar(c)) {
  37.             return true;
  38.          } else {
  39.             switch (this.maskCompiled[index]) {
  40.                case '#':
  41.                   return Character.isDigit(c);
  42.                case '&':
  43.                   return true;
  44.                case '9':
  45.                   return this.isequalPromptChar(c) || Character.isDigit(c) || c == '+' || c == '-' || c == ' ';
  46.                case '?':
  47.                   return this.isequalPromptChar(c) || Character.isLetter(c);
  48.                case 'A':
  49.                   return Character.isLetterOrDigit(c);
  50.                case 'L':
  51.                   if (this.maskEmpty[index] != c) {
  52.                      return false;
  53.                   }
  54.  
  55.                   return true;
  56.                case 'a':
  57.                   return this.isequalPromptChar(c) || Character.isLetterOrDigit(c) || c == ' ';
  58.                default:
  59.                   return false;
  60.             }
  61.          }
  62.       } else {
  63.          throw new IllegalArgumentException("Illegal Index");
  64.       }
  65.    }
  66.  
  67.    public char checkCase(int index, char c) throws IllegalArgumentException {
  68.       if (index >= 0 && index < this.length()) {
  69.          switch (this.maskCase[index]) {
  70.             case '?':
  71.             default:
  72.                break;
  73.             case 'L':
  74.                c = Character.toLowerCase(c);
  75.                break;
  76.             case 'U':
  77.                c = Character.toUpperCase(c);
  78.          }
  79.  
  80.          return c;
  81.       } else {
  82.          throw new IllegalArgumentException("Illegal Index");
  83.       }
  84.    }
  85.  
  86.    public String getEmptyMask() {
  87.       return new String(this.maskEmpty, 0, this.length());
  88.    }
  89.  
  90.    public Mask() {
  91.       this("", '_');
  92.    }
  93.  
  94.    public Mask(String mask) {
  95.       this(mask, '_');
  96.    }
  97.  
  98.    public Mask(String mask, char promptChar) {
  99.       this.len = 0;
  100.       this.mask = mask;
  101.       this.setPromptChar(promptChar);
  102.    }
  103.  
  104.    public boolean isMaskSet() {
  105.       return this.length() != 0;
  106.    }
  107.  
  108.    public String toString() {
  109.       return "{mask=\"" + this.mask + "\",promptChar='" + this.promptChar + "'}";
  110.    }
  111.  
  112.    public char getPromptChar() {
  113.       return this.promptChar;
  114.    }
  115.  
  116.    public boolean isequalPromptChar(char test) {
  117.       return test == this.getPromptChar();
  118.    }
  119.  
  120.    public void setPromptChar(char promptChar) {
  121.       if (promptChar == 0) {
  122.          this.promptChar = ' ';
  123.       } else {
  124.          this.promptChar = promptChar;
  125.       }
  126.  
  127.       this.maskCompile();
  128.    }
  129.  
  130.    private void maskCompile() {
  131.       this.maskCompiled = new char[this.mask.length()];
  132.       this.maskCase = new char[this.mask.length()];
  133.       this.maskEmpty = new char[this.mask.length()];
  134.       this.len = 0;
  135.       char caseState = '?';
  136.  
  137.       for(int i = 0; i < this.mask.length(); ++i) {
  138.          char c = this.mask.charAt(i);
  139.          switch (c) {
  140.             case '#':
  141.             case '&':
  142.             case '9':
  143.             case '?':
  144.             case 'A':
  145.             case 'a':
  146.                this.maskEmpty[this.len] = this.getPromptChar();
  147.                this.maskCompiled[this.len] = c;
  148.                this.maskCase[this.len++] = caseState;
  149.                break;
  150.             case '<':
  151.                caseState = 'L';
  152.                break;
  153.             case '>':
  154.                caseState = 'U';
  155.                break;
  156.             case '\\':
  157.                ++i;
  158.             default:
  159.                this.maskEmpty[this.len] = c;
  160.                this.maskCompiled[this.len] = 'L';
  161.                this.maskCase[this.len++] = '?';
  162.          }
  163.       }
  164.  
  165.    }
  166.  
  167.    public String getMask() {
  168.       return new String(this.mask);
  169.    }
  170.  
  171.    public void setMask(String mask) {
  172.       this.mask = mask;
  173.       this.maskCompile();
  174.    }
  175.  
  176.    public int skipLiteral(int index, int direction) {
  177.       if (index >= 0 && index < this.length()) {
  178.          switch (direction) {
  179.             case 1:
  180.                while(index > 0 && this.maskCompiled[index] == 'L') {
  181.                   --index;
  182.                }
  183.                break;
  184.             case 2:
  185.                while(index < this.length() && this.maskCompiled[index] == 'L') {
  186.                   ++index;
  187.                }
  188.          }
  189.       }
  190.  
  191.       return index;
  192.    }
  193.  
  194.    public int length() {
  195.       return this.len;
  196.    }
  197. }
  198.